<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<view class="u-flex-col u-col-center money-box">
			<!-- <text class="time" v-show="isCountDown">{{ timeText }}</text> -->
			<view class="money">{{ price.toFixed(2)}}</view>
		</view>

		<!-- 支付方式单选项 -->
		<u-radio-group v-if="paymentList && paymentList.length" class="pay-box" v-model="payType"
			active-color="#f0c785">
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('wechat')"
				@tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wx_pay.png'" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"  ></u-radio>
			</view>
			<!-- #ifdef APP-PLUS  -->
			<!-- 支付宝支付 -->
			<view class="u-flex u-row-between pay-item" v-show="paymentList.includes('alipay')"
				@tap="payType = 'alipay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_ali_pay.png'" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<u-radio shape="circle" name="alipay"></u-radio>
			</view>
            <!--  #endif -->
			 
		</u-radio-group>

		<button class="u-reset-button pay-btn" @tap="confirmPay">确认支付
			￥{{ price.toFixed(2) || '0.00' }}</button>
	</view>
</template>

<script>
	/**
	 * 接收商品订单orderType:goods，充值订单orderType:recharge
	 */
	// import Paycart from '@/ljshop/paycart';
	import Pay from '@/ljshop/pay';
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	let timer;
	export default {
		components: {},
		data() {
			return {
				payType: 'wechat', //支付方式
				isCountDown: true, //是否显示订单倒计时。
				orderDetail: {},
				timeText: '', //倒计时文本
				platform: this.$platform.get(),
				appPlatfrom: '',
				orderType: 'goods',
				paymentList: ["alipay", "wechat", "wallet"],
				orderList:[],
				price:0
			};
		},

		onShow() {
			//this.payType = this.initPayment?. [0];
		//	this.orderDetail.id && this.countDown();
		},
		onLoad() {
			this.orderType = this.$Route.query.orderType
			if (this.$platform.get() === 'wxOfficialAccount' && this.$platform.device() === 'ios' && this.$platform
				.entry() !== location.href) location.reload();
			// 获取订单详情
			this.getOrderDetail()
		},
		onHide() {
			clearInterval(timer);
			timer = null;
		},
		methods: {
			// 获取订单详情
			getOrderDetail() {
				this.getGoodsOrderDetail()
			},
			// 倒计时
			countDown() {
				let that = this;
				let t = parseInt(that.orderDetail.expired_time * 1000) - parseInt(new Date().getTime());
				t = t / 1000;
				timer = setInterval(() => {
					if (t > 0) {
						let time = that.$tools.format(t);
						that.timeText = `支付剩余时间 ${time.m}:${time.s}`;
						t--;
					} else {
						clearInterval(timer);
						that.timeText = '订单已过期!';
					}
				}, 1000);
			},

			// 发起支付
			confirmPay() {
				let that = this;
				let pay = null;
				if (!that.payType) {
					this.$u.toast('请选择支付方式');
					return;
				}
				if (that.payType === 'wallet') {
					// uni.showModal({
					// 	title: '支付提示',
					// 	confirmColor: '#f0c785',
					// 	content: `是否确认使用余额支付:${ this.orderDetail.price }元?`,
					// 	success: res => {
					// 		if (res.confirm) {
					// 			pay = new Paycart(that.payType, that.orderList, that.$Route.query.orderType);
					// 		}
					// 	}
					// });
				} else {
					// pay = new Paycart(that.payType, that.orderList, that.$Route.query.orderType);
					let Idlist = that.$Route.query.orderId;
					let Idstr=""
					for(let i=0;i<Idlist.length;i++){
						Idstr = Idstr+","+Idlist[i]
					}
					Idstr =Idstr.substring(1,Idstr.length)
					console.log(Idstr)
					let obj = {
						id:Idstr,
						amount:0,
						outTradeNo:that.$Route.query.outTradeNo
					}
					for(var i in that.orderList){
						obj.amount += that.orderList[i].amount
					}
				
					pay = new Pay(that.payType, obj, 'cart');
				}
			},

			// 支付商品订单信息
			getGoodsOrderDetail() {
				let that = this;
				let Idlist = that.$Route.query.orderId;
				let Idstr=""
				for(let i=0;i<Idlist.length;i++){
					Idstr = Idstr+","+Idlist[i]
				}
				Idstr =Idstr.substring(1,Idstr.length)
				that.$http('order.sndetail', {
					snOrder: Idstr
				}).then(res => {
					if (res.code == 1) {
						let price = 0;
						that.orderList = res.data.orders;
						for(var i in res.data.orders){
							that.price += res.data.orders[i].amount
						}
						// if (res.order.ext_arr !== null) {
						// 	that.countDown();
						// } else {
						// 	that.isCountDown = false;
						// }
					}
				});
			},

			// 充值订单信息
			getRechargeOrderDetail() {
				let that = this;
				that.$http('money.rechargeDetail', {
					id: that.$Route.query.orderId
				}).then(res => {
					if (res.code === 1) {
						that.orderDetail = res.data;
						// if (res.data.ext_arr !== null) {
						// 	that.countDown();
						// } else {
						// 	that.isCountDown = false;
						// }
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.money-box {
		background: #fff;
		height: 250rpx;
		margin-bottom: 20rpx;
		padding-top: 30rpx;

		.time {
			font-size: 28rpx;
			color: #c4c4c4;
		}

		.money {
			color: #e1212b;
			font-size: 60rpx;
			margin-top: 60rpx;

			&::before {
				content: '￥';
				font-size: 46rpx;
			}
		}
	}

	.pay-box {
		.pay-item {
			height: 90rpx;
			padding: 0 50rpx;
			font-size: 26rpx;
			background: #fff;
			width: 750rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border-bottom: none;
			}

			.pay-img {
				width: 40rpx;
				height: 40rpx;
				margin-right: 25rpx;
			}
		}
	}

	.pay-btn {
		width: 690rpx;
		line-height: 80rpx;
		background: linear-gradient(90deg, rgba(240, 199, 133, 1), rgba(246, 214, 157, 1));
		border-radius: 40rpx;
		color: rgba(#fff, 0.9);
		margin: 100rpx auto 0;
	}
</style>
